package model.map;

import model.entity.*;

public class AvatarStateVisitor extends MapVisitor {
	private Avatar avatar;
	/**
	 * Constructor for an AvatarStateVisitorRange.
	 * @param avatar
	 */
	public AvatarStateVisitor(Avatar avatar) {
		this.avatar = avatar;
	}

	/**
	 * Updates the states of everything within the avatar's
	 * range.
	 */
	public void visit(GameMap gameMap) {
		Avatar avatar = this.avatar;
		
		MapPoint avatarLocation = avatar.getLocation();
		MapPoint avatarDirection = avatar.getDirection();
		int range = avatar.getViewRange();
		
		MapPoint startPoint = MapPoint.scaleDirection(avatarDirection, range+1);
		startPoint = MapPoint.addPoints(startPoint, avatarLocation);
		
		int xHighBound = Math.min(avatarLocation.getXCoordinate() + 2 * range,
				gameMap.getBoundary().getXCoordinate());
		int xLowBound = Math.max(avatarLocation.getXCoordinate() - 2 * range, 0);
			
		int yHighBound = Math.min(avatarLocation.getYCoordinate() + range, 
				gameMap.getBoundary().getYCoordinate());
		int yLowBound = Math.max(avatarLocation.getYCoordinate() - range, 0);
		
		// iterate over all of the points to change its state
		for(int i = xLowBound;i<=xHighBound;++i){
			for(int j = yLowBound;j<=yHighBound;++j){
				MapPoint test = new MapPoint(i,j);
				if(!MapPoint.isValidLocation(test))
					continue;
				if(gameMap.getTileState(test) == null)
					gameMap.setTileState(test, new CurrentlyViewable(gameMap,test));
				else{
					MapTileState state = gameMap.getTileState(test);
					state.updateState();
				}
			}
		}
	}
}


